<?php
namespace app\mobile\controller;

use app\home\logic\UsersLogic;
use app\home\model\Message;
use think\Page;
use think\Request;
use think\Verify;
use think\db;
use think\Session;
use phpDocumentor\Reflection\DocBlock\Tag\VarTag;

class Station extends MobileBase
{
    public $user;
    public $user_id;
    /*
     * 初始化操作
     */
    public function _initialize()
    {
        parent::_initialize();
        if (session('?user')) {
            $user = session('user');
            $user = M('users')->where("user_id", $user['user_id'])->find();
            session('user', $user);  //覆盖session 中的 user
            if($user['pay_gold']>100 && !empty($user['pay_gold'])){
                $user['pay_gold_TX'] = number_format($user['pay_gold'] * 0.3, 2, '.', '');
            }
            else{
                $user['pay_gold_TX'] = '0.00';
            }
            $station_info = M('station')->where('user_id',$user['user_id'])->find();
            $this->assign('station_info',$station_info);
            $this->user = $user;
            $this->user_id = $user['user_id'];
            $this->assign('user', $user); //存储用户信息
        }
        
        $nologin = array(
            'login', 'pop_login', 'do_login', 'logout', 'verify', 'set_pwd', 'finished',
            'verifyHandle', 'reg', 'send_sms_reg_code', 'find_pwd', 'check_validate_code',
            'forget_pwd', 'check_captcha', 'check_username', 'send_validate_code', 'express',
        );
        if (!$this->user_id && !in_array(ACTION_NAME, $nologin)) {
            header("location:" . U('Mobile/User/login'));
            exit;
        }
    } 
    
    
    public function index(){
       if($this->user["station_id"]){
           header("location:" . U('Mobile/station/station_info',array('station_id'=>$this->user["station_id"])));
           exit();
       }
       //查询省列表
       $province_list = M("region")->where("level",1)->select();
       $this->assign("province_list",$province_list);
       return $this->fetch();
    }
    
    //查询区域列表
    public function get_region(){
        $level = I("post.level");
        if($level == 1){//省份
            $parent_id = I("post.province_id");
            $where['province'] = $parent_id;
        }elseif($level == 2){//市
            $parent_id = I("post.city_id");
            $where['city'] = $parent_id;
        }else{//区县
            $parent_id = I("post.district_id");
            $where['district'] = $parent_id;
        }
        $where['status'] = 1;
        //查询地区
        $result['region_list'] = M("region")->where("parent_id",$parent_id)->select();
        $result['station_count'] =M("station")->where($where)->count();
        //查询地图内的品酒站数量
        $back['status'] = 1;
        $back['msg'] = "查询成功";
        $back['data'] = $result;
        echo json_encode($back);
    }
    
    
    //申请建站
    public function create_station(){
        $where['province'] = I("province")?I("province"):0;
        $where['city'] = I("city")?I("get.city"):0;
        $where['district'] = I("district")?I("district"):0;
        if(empty($where['province'])){
            $this->error("错误的访问方式",U("/mobile/user"));
        }
        //查询该地区已有站点数
        $where['status'] = 1;
        $station_count = M("station")->where($where)->count();
        if($station_count > 2){
            $this->error("本地图已存在三个俱乐部，请申请其他地区",U("/mobile/station"));
        }
        if(IS_POST){//建站提交
            //判断当前用户是否已申请过建站
            $statin_info = M("station")->where("user_id=$this->user_id and status < 2")->count();
            if($statin_info){
                $this->error("您已申请过品酒俱乐部");
            }
            $data['user_name'] = I("post.user_name");
            $data['user_id'] = $this->user_id;
            $data['user_mobile'] = I("post.user_mobile");
            $data['user_email'] = I("post.user_email");
            $data['user_weixin'] = I("post.user_weixin");
            $data['name'] = I("post.club_name");
            $data['des'] = I("post.desc");
            $data['province'] = I("post.province");
            $data['city'] = I("post.city");
            $data['district'] = I("post.district");
            $data['addtime'] = time();
            $data['is_lock'] = 0;
            $data['status'] = 0;
            $result = M("station")->add($data);
            if($result){
                $data1['station_id'] = $result;
                $data1['station_time'] = time();
                $data1['station_status'] = 1;
                M("users")->where("user_id",$this->user_id)->save($data1);
                $this->success("资料提交成功,请等待系统审核",U("/mobile/station/station_info"));
            }else{
                $this->error("资料提交失败,请重试");
            }
        }
        //查询地图名称
        $name = $this->getAddressName($where['province'],$where['city'],$where['district']);
        $addressname = explode(",",$name);
        if($addressname[2]){
            $addressname1 = $addressname[2];
        }else if($addressname[1]){
            $addressname1 = $addressname[1];
        }else{
            $addressname1 = $addressname[0];
        }
        $this->assign("province",$where['province']);
        $this->assign("city",$where['city']);
        $this->assign("district",$where['district']);
        $this->assign("addressname",$addressname);
        $this->assign("addressname1",$addressname1);
        $this->assign("user_id",$this->user_id);
        return $this->fetch();
    }
    
    //申请建站列表
    public function create_station_list(){
        //查询当前用户是否已申请过建站
        $station_info = M("station")->where("user_id",$this->user_id)->order("id desc")->find();
        if($station_info){
            //查询当前地区内的品酒站
            $where['province'] = $station_info['province'];
            $where['city'] = $station_info['city'];
            $where['district'] = $station_info['district'];
            $station_list = M("station")->where($where)->select();
//             foreach ($station_list as $key=>$value){
//                 //查询站长推广人员(已验证手机号)
//                 $distribut_info = M('users')->where("first_leader=".$value['user_id']." and mobile_validated = 1")->select();
//                 $station_list[$key]['dirtribut_count'] = count($distribut_info);
//                 //合格人数
//                 $qualified_count = 0;
//                 //站长资格总计人数(下线人数+下线发展人数)
//                 $station_list[$key]['dirtribut_count_second'] = $station_list[$key]['dirtribut_count'];
//                 //查询推广人员中的合格人员(下线在发展下线30人)
//                 foreach ($distribut_info as $k=>$v){
//                     //查询该下线发展的下线人数
//                     $distribut_count1 = M("users")->where("first_leader=".$v['user_id']." and mobile_validated = 1")->count();
//                     $station_list[$key]['dirtribut_count_second'] +=  $distribut_count1;
//                     if($distribut_count1 >=30){//发展下线30人
//                         $qualified_count++;
//                     }
//                 }
//                 $station_list[$key]['qualified_count'] = $qualified_count;
//             }
//             sort_array($station_list,"dirtribut_count_second","desc");
            if($station_list){
                foreach ($station_list as $key=>$value){
                    //统计当前俱乐部人数(验证手机用户才算)
                    $station_list[$key]['station_merber_count'] = M("users")->where("station_id = ".$value['id']." and mobile_validated = 1")->count();
                }
                //按俱乐部统计人数排序
                sort_array($station_list,"station_merber_count","desc");
            }   
        }else{
            $this->error("未申请过建立品酒俱乐部,无权查看",U("/mobile/station"));
        }
        $this->assign("station_list",$station_list);
        return $this->fetch();
    }
    
    
    //站点列表
    public function join_station(){
        $where['province'] = I("province")?I("province"):0;
        $where['city'] = I("city")?I("get.city"):0;
        $where['district'] = I("district")?I("district"):0;
        $where['province'] = 10543;
        $where['city'] = 10544;
        $where['district'] = 10592;
        if(empty($where['province'])){
            $this->error("错误的访问方式",U("/mobile/user"));
        }
        $where['is_lock'] = 0;
        $station_list = M("station")->where($where)->select();
        if($station_list){
            foreach ($station_list as $key=>$value){
                //查询各站成员
                $station_user_list = M("users")->where("station_id",$value['id'])->select();
                //查询各站人数
                $station_list[$key]['member_count'] = count($station_user_list);
                //查询各站业绩
                $station_list[$key]['performance_count'] = get_station_income($value['id']);
            }
        }
       
        $this->assign("station_list",$station_list);
        return $this->fetch();
    }
    
    
    /**
     * 获取地区名字
     * @param int $p
     * @param int $c
     * @param int $d
     * @return string
     */
    public function getAddressName($p=0,$c=0,$d=0){
        $p = M('region')->where(array('id'=>$p))->field('name')->find();
        $c = M('region')->where(array('id'=>$c))->field('name')->find();
        $d = M('region')->where(array('id'=>$d))->field('name')->find();
        return $p['name'].','.$c['name'].','.$d['name'].',';
    }
    
    //申请入站
    public function apply_join_station(){
        if(IS_AJAX){
            $station_id = I("post.station_id");
            //查询该用户是否已申请过此站
            if($this->user['station_id'] && $this->user['station_status'] != 2){
                $back['status'] = 1;
                $back['msg'] = "您已有加入申请,请耐心等待";
                $back['data'] = "您已有加入申请,请耐心等待";
            }else{
                $data['station_id'] = $station_id;
                $data['station_status'] = 0;
                $result = M("users")->where("user_id",$this->user_id)->save($data);
                if($result){
                    $back['status'] = 1;
                    $back['msg'] = "申请成功,请耐心等待";
                    $back['data'] = $result;
                }else{
                    $back['status'] = 0;
                    $back['msg'] = "申请失败,请重试";
                    $back['data'] = "申请失败,请重试试";
                }
            }
        }else{
            $back['status'] = 0;
            $back['msg'] = "非法访问";
            $back['data'] = "非法访问";
        }
        echo json_encode($back);
        exit();
    }
    
    //我的品酒站
    public function station_info(){
//     	if(!$this->user['station_id']){
//     		$this->error("您尚未加入任何俱乐部！",U("/mobile/station"));
//     	}
        $station_id = I("station_id/d");
        $is_station_member = 1;//判断该用户是否有站
        if($this->user['station_status'] == 1){//有站
            if($this->user['station_id'] != $station_id){
                $this->error("您已有俱乐部",U('Mobile/Station/station_info',array('station_id'=>$this->user['station_id'])));
//                 header("Location:" . U('Mobile/Station/station_info',array('station_id'=>$this->user['station_id'])));
            }
        }else{//无站
            $is_station_member = 0;
            if(I('get.distribut_id')){//绑定邀请人
                if(empty($this->user['station_id'])){
                    $data['station_id'] = $station_id;
                    $data['station_leader'] = I('get.distribut_id');
                    $data['station_status'] = 0;
                    M("users")->where("user_id",$this->user_id)->save($data);
                }
            }
        }

        $station_info = M("station")->where("id",$station_id)->find();
        if(!$station_info){
        	$this->error("该品酒俱乐部不存在或已解散！",U("/mobile/station"));
        }
        //判断当前用户是否为站长
        $is_captain = 0;
        if($this->user_id == $station_info['user_id']){
            $is_captain = 1; 
        }
        //查询本站人数
        $station_info['member_count'] = M("users")->where("station_id",$station_id)->count();
        $year = date("Y");
        $month = date("m");
        $allday = date("t");
        $strat_time = strtotime($year."-".$month."-1");//获取本月第一天
        $end_time = strtotime($year."-".$month."-".$allday);//获取本月自后一天
        //查询本站利润
        $station_income = get_station_income($station_id);//总收益
        $station_income_month_now = get_station_income($station_id,$strat_time,$end_time);//本月收益
        $station_info['station_income'] = $station_income;
        $station_info['station_income_month_now'] = $station_income_month_now;
        
        
        $this->assign("is_station_member",$is_station_member);
        $this->assign("is_captain",$is_captain);
        $this->assign("station_info",$station_info);
        return $this->fetch(); 
    }
    
    //申请品酒会
    public function create_station_party(){
        //判断当前用户是否为站长
        $station_info = M("station")->where("user_id = $this->user_id and is_lock = 0")->find();
        $this->is_captain();
        if(IS_POST){
            $data['user_id'] = $this->user_id;
            $data['station_id'] = $station_info['id'];
            $data['user_name'] = $station_info['user_name'];
            $data['user_num'] = I("post.people");
            $data['hold_time'] = strtotime(I("post.start_time"));
            $data['end_time'] = strtotime(I("post.end_time"));
            $data['province'] = $station_info['province'];
            $data['city'] = $station_info['city'];
            $data['district'] = $station_info['district'];
            $data['party_name'] = date("Y年m月d日",$data['hold_time']).$station_info['name']."品酒会";
            $data['address'] = I("post.address");
            $data['brand'] = I("post.brand");
            $data['degrees'] = I("post.degrees");
            $data['num'] =  I("post.num");
            $data['apply_des'] =  I("post.apply_des");
            $data['add_time'] =  time();
            $data['stattus'] = 0;
            $result = M("station_party")->add($data);
            if($result){
                $this->success("申请成功",U("/mobile/station/station_info"));
            }else{
                $this->error("申请失败,请重试");
            }
        }
        //查询地图名称
        $name = $this->getAddressName($station_info['province'],$station_info['city'],$station_info['district']);
        $addressname = explode(",",$name);
        $this->assign("addressname",$addressname);
        $this->assign("station_info",$station_info);
        return $this->fetch();
    }
    
    //申请参与品酒会
    public function join_station_party(){
        //查询本站是否有品酒会 
        $party_info = M("station_party")->where("station_id =".$this->user['station_id']." and status=1 and end_time>".time())->order("id desc")->find();
        if(empty($party_info)){
            $this->error("本俱乐部暂无品酒会举办",U("/mobile/station/station_info"));
        }
        if(IS_POST){//申请参与品酒会
            //判断当前用户是否已申请过参与
            $applu_info=M("station_party_user")->where("party_id =".$party_info['id']." and user_id =".$this->user_id." and status !=2")->find();
            if($applu_info){
                $this->success("申请成功",U("/mobile/station/station_info"));
            }
            $data['user_id'] = $this->user_id;
            $data['party_id'] = $party_info['id'];
            $data['user_name'] = I("post.user_name");;
            $data['user_year'] = I("post.user_year");
            $data['mobile'] = I("post.mobile");;
            $data['company'] = I("post.company");
            $data['job'] = I("post.job");
            $data['aroma'] =  I("post.aroma");
            $data['brand'] =  I("post.brand");
            $data['degrees'] =  I("post.degrees");
            $data['add_time'] =  time();
            $data['stattus'] = 0;
            $result = M("station_party_user")->add($data);
            if($result){
                $this->success("申请成功",U("/mobile/station/station_info"));
            }else{
                $this->error("申请失败,请重试");
            }
        }
        $this->assign("user_id",$this->user_id);
        return $this->fetch();
    }
    
    //品酒会申请列表
    public function party_apply_list() {
       //判断当前用户是否为站长
        $station_info = M("station")->where("user_id = $this->user_id")->find();
        $this->is_captain();
        //查询本站是否有品酒会
        $party_info = M("station_party")->where("station_id =".$this->user['station_id']." and stattus=1 and end_time>".time())->order("id desc")->find();
        if(empty($party_info)){
            $this->error("本俱乐部暂无品酒会举办",U("/mobile/station/station_info"));
        }
        //查询本次品酒会申请参与人信息
        $user_list = M("station_party_user")->where("party_id",$party_info['id'])->order("status")->select();
        $this->assign("user_list",$user_list);
        return $this->fetch();
    }
    
    //申请列表操作
    public function party_apply_opreate(){
        if(IS_AJAX){
            $where['id'] = I("post.party_apply_id");
            $data['status'] = I("post.status");
            $result = M("station_party_user")->where($where)->save($data);
            if($result){
                $back['status'] = 1;
                $back['msg'] = "操作成功";
                $back['data'] = $result;
            }else{
                $back['status'] = 0;
                $back['msg'] = "操作失败,请重试";
                $back['data'] = "操作失败,请重试试";
            }
        }else{
            $back['status'] = 0;
            $back['msg'] = "非法访问";
            $back['data'] = "非法访问";
        } 
        echo json_encode($back);
        exit();
    }
    
    //往期品酒会记录
    public function station_party_list(){
        //判断当前用户是否为站长
        $station_info = M("station")->where("user_id = $this->user_id")->find();
        $this->is_captain();
        $station_party_list = M("station_party")->where("station_id",$this->user['station_id'])->select();
        foreach ($station_party_list as $key=>$value){
            $station_party_list[$key]['peoples'] = M("station_party_user")->where("party_id",$value['id'])->count();
        }
        $this->assign("station_party_list",$station_party_list);
        return $this->fetch();
    }
    
    //我参与的品酒会
    public function my_party_list(){
        $station_party_list = M("station_party")->where("station_id =".$this->user['station_id']." and user_id = $this->user_id")->select();
        foreach ($station_party_list as $key=>$value){
            $station_party_list[$key]['peoples'] = M("station_party_user")->where("party_id",$value['id'])->count();
        }
        $this->assign("station_party_list",$station_party_list);
        return $this->fetch();
    }
    
    //俱乐部成员
    public function station_member(){
        
        //查询当前俱乐部信息
        $station_info = M("station")->where("id",$this->user['station_id'])->find();
        //判断当前用户是否为站长
//         $station_info = M("station")->where("user_id = $this->user_id")->find();
        //判断当前用户是否为站长
        $is_captain = 0;
        if($this->user_id == $station_info['user_id']){
            $is_captain = 1;
        }
        //助理
        $assistant_array = json_decode($station_info['assistant_id'],true);
        if(empty($assistant_array)){
            $assistant_array = array();
        }
        //站长管理所有用户
        if($is_captain){
            //查询当前俱乐部所有成员
            $member_list = M("users")->where("station_id =".$this->user['station_id']." and station_status != 2")->order("station_status")->select();
        }else{//筛选已审核通过的成员
            $member_list = M("users")->where("station_id = ".$this->user['station_id']." and station_status = 1")->select();
        }
        //计算本月月初与月底
        $year = date("Y");
        $month = date("m");
        $allday = date("t");
        $strat_time = strtotime($year."-".$month."-1");//获取本月第一天
        $end_time = strtotime($year."-".$month."-".$allday);//获取本月自后一天
        $identity = array();
        foreach ($member_list as $key=>$value){
//          $distrbut_count = M("users")->where("first_leader",$value['user_id'])->count();
            //统计推客
            $member_list[$key]['station_distribut'] = M("users")->where("station_leader = ".$value['user_id']." and mobile_validated = 1 and station_id = ".$this->user['station_id']." and station_status = 1")->count();
            //统计当月消费总额
            $member_list[$key]['income_count'] = get_station_member_income($value['user_id'],$strat_time,$end_time);
            //统计该成员本月消费金额
            if(empty($value['station_status'])){
                $member_list[$key]['identity'] = 0;//未入站成员
            }elseif($value['user_id'] == $station_info['user_id']){
                $member_list[$key]['identity'] = 1;//秘书长
            }elseif(in_array($value['user_id'] ,$assistant_array)){
                $member_list[$key]['identity'] = 2;//助理
            }else{
                if($member_list[$key]['station_distribut'] >=20){
                    $member_list[$key]['identity'] = 3;//推客满20人的成员
                }else{
                    $member_list[$key]['identity'] = 4;//普通成员
                }
            }
            $identity[] = $member_list[$key]['identity'];
        }
        //按身份排序
        array_multisort($identity, SORT_ASC, $member_list);
        
        //将成员按消费金额排序
        $arr1 = array();//管理成员
        $arr2 = array();//成员
        foreach ($member_list as $k=>$v){
            if($is_captain){//站长
                if($v['identity'] > 3){
                    array_push($arr2,$v);
                }else{
                    array_push($arr1,$v);
                }
            }else{//成员
                if($v['identity'] > 2){
                    array_push($arr2,$v);
                }else{
                    array_push($arr1,$v);
                }
            }
        }
        //将成员按消费金额排序
        if(!empty($arr2)){
            sort_array($arr2,"income_count","desc");
        }

        $member_list = array_merge($arr1,$arr2);
        $this->assign("is_captain",$is_captain);
        $this->assign("member_list",$member_list);
        return $this->fetch();
    }
    
    //我的推客
    public function ditribut_list(){
        //查询我的下线
        $ditribut_list = M("users")->where("first_leader=".$this->user_id." and mobile_validated = 1")->select();
        if($ditribut_list){
            foreach ($ditribut_list as $key=>$value){
                //统计各下线的下线人数
                $ditribut_list[$key]['ditribut_count'] = M("users")->where("first_leader =".$value['user_id']." and mobile_validated = 1")->count();
            }
            sort_array($ditribut_list,"ditribut_count","desc");
        }
        $this->assign("ditribut_list",$ditribut_list);
        return $this->fetch();
    }
    
    //清除用户站点信息
    public function clear_station(){
        $data['station_id'] = 0;
        $data['station_time'] = 0;
        $data['station_status'] = 0;
        $result = M("users")->where("user_id",$this->user_id)->save($data);
        if($result){
            $back['status'] = 1;
            $back['msg'] = "操作成功";
            $back['data'] = $result;
        }
        echo  json_encode($back);
        exit();
    }
    //入站操作
    public function station_member_oprata(){
        if(IS_AJAX){
            $station_info = M("station")->where("user_id = $this->user_id and is_lock = 0")->find();
            if(empty($station_info)){
                $back['status'] = 0;
                $back['msg'] = "您不是秘书长,无权操作";
                $back['data'] = "非法操作";
                echo json_encode($back);
                exit();
            }
            $assistant_array = json_decode($station_info['assistant_id'],true);
            $where['user_id'] = I("post.user_id");
            //查询该用户信息
            $user_info = M("users")->where("user_id",$where['user_id'])->find();
            $type = I("post.type");
            switch ($type){
                case 1://同意入站
                    $data['station_id'] = $this->user['station_id'];
                    $data['station_time']=time();
                    $data['station_status'] = 1;
                    //记录入站日志
                    $station_action = array();
                    $action = array(
                        "act_time"=>time(),
                        "station_id"=>$this->user['station_id'],
                        "type"=>1,
                        "act_name"=>"加入品酒俱乐部"
                    );
                    if($user_info['station_action']){
                        $station_action = json_decode($user_info['station_action'],true);
                    }
                    array_push($station_action,$action);
                    $data['station_action'] = json_encode($station_action);
                    $result = M("users")->where($where)->save($data);
                    if($result){
                        $back['status'] = 1;
                        $back['msg'] = "操作成功";
                        $back['data'] = $result;
                    }else{
                        $back['status'] = 0;
                        $back['msg'] = "操作失败,请重试";
                        $back['data'] = "操作失败,请重试试";
                    }
                    break;
                case 2://拒绝入站
                    $data['station_id'] = $this->user['station_id'];
                    $data['station_status'] = 2;
                    $result = M("users")->where($where)->save($data);
                    if($result){
                        $back['status'] = 1;
                        $back['msg'] = "操作成功";
                        $back['data'] = $result;
                    }else{
                        $back['status'] = 0;
                        $back['msg'] = "操作失败,请重试";
                        $back['data'] = "操作失败,请重试试";
                    }
                    break;
                case 3://提拔为助理
                    if(count($assistant_array) >= 5){
                        $back['status'] = 0;
                        $back['msg'] = "操作失败,助理最大上线为5个";
                        $back['data'] = "操作失败,助理最大上线为5个";
                        echo json_encode($back);
                        exit();
                    }
                    $ditribut_count = M("users")->where("first_leader",$where['user_id'])->count();
                    if($ditribut_count < 30){
                        $back['status'] = 0;
                        $back['msg'] = "操作失败,该成员不满足资格";
                        $back['data'] = "推客少于30人";
                        echo json_encode($back);
                        exit();
                    }
                    $assistant_array[] = $where['user_id'];
                    $result = M("station")->where("id",$this->user['station_id'])->save(array("assistant_id"=>json_encode($assistant_array)));
                    if($result){
                        $back['status'] = 1;
                        $back['msg'] = "操作成功";
                        $back['data'] = $result;
                    }else{
                        $back['status'] = 0;
                        $back['msg'] = "操作失败,请重试";
                        $back['data'] = "操作失败,请重试试";
                    }
                    break;
                case 4://取消助理资格
                    if(is_array($assistant_array)){
                        if(in_array($where['user_id'],$assistant_array)){
                            foreach ($assistant_array as $key=>$value){
                                if($value == $where['user_id']){
                                    unset($assistant_array[$key]);
                                }
                            }
                            $result = M("station")->where("id",$this->user['station_id'])->save(array("assistant_id"=>json_encode($assistant_array)));
                            if($result){
                                $back['status'] = 1;
                                $back['msg'] = "操作成功";
                                $back['data'] = $result;
                            }else{
                                $back['status'] = 0;
                                $back['msg'] = "操作失败,请重试";
                                $back['data'] = "操作失败,请重试试";
                            }
                        }else{
                            $back['status'] = 0;
                            $back['msg'] = "操作失败,该成员不是助理";
                            $back['data'] = "操作失败,该成员不是助理";
                        }
                    }else{
                        $back['status'] = 0;
                        $back['msg'] = "操作失败,该成员不是助理";
                        $back['data'] = "操作失败,该成员不是助理";
                    }
                    break;                 
            }
        }else{
            $back['status'] = 0;
            $back['msg'] = "非法访问";
            $back['data'] = "非法访问";
        } 
        echo json_encode($back);
        exit();
    }
    
    //本月业绩
    public function station_income_detail(){
        //计算本月月初与月底
        $year = date("Y");
        $month = date("m");
        $allday = date("t");
        $strat_time = strtotime($year."-".$month."-1");//获取本月第一天
        $end_time = strtotime($year."-".$month."-".$allday);//获取本月自后一天
        //查询所有订单，级联查询消费者信息
        $order_list = M("order")
        ->alias("o")
        ->join("f_users u","o.user_id = u.user_id")
        ->where("confirm_time > $strat_time and confirm_time < $end_time and order_status in(2,4)")
        ->field("o.*,u.nickname,u.station_id")
        ->order("confirm_time desc")
        ->select();
        //筛选出本站订单
        $station_order_list = array();
        foreach ($order_list as $key=>$value){
            if($value['station_id'] == $this->user['station_id']){
                array_push($station_order_list,$value);
            }
        }
        $this->assign("station_order_list",$station_order_list);
        return $this->fetch(); 
    }
    
    public function station_funds_apply_detail(){
        $station_fund_apply = M("station_fund_apply")->where("station_id =".$this->user['station_id'])->order("add_time desc")->select();
        $this->assign("station_fund_apply",$station_fund_apply);
        return $this->fetch(); 
    }
    
    
    public function station_funds_detail(){
        $station_fund_apply = M("station_fund_apply")->where("station_id =".$this->user['station_id']." and status = 1")->order("add_time desc")->select();
        $this->assign("station_fund_apply",$station_fund_apply);
        return $this->fetch(); 
    }
    //俱乐部经费申请经费
    public function station_funds_apply(){
//         $this->is_captain();
        $station_info = M("station")->where("user_id = $this->user_id")->find();
        if(IS_POST){
            $data['station_id'] = $station_info['id'];
            $data['start_time'] = strtotime(I("post.start_time"));
            $data['source'] = I("post.source");
            $data['money'] = I("post.money");
            $data['add_time'] = time();
            $data['des'] = I("post.des");
            $data['status'] = 0;
            $result =  M("station_fund_apply")->add($data);
            if($result){
                $this->success("申请成功,等待系统审核",U("/mobile/station/station_funds_apply_detail"));
            }else{
                $this->error("申请失败,请重试");
            }
        }
        $this->assign("station_info",$station_info);
        return $this->fetch(); 
    }
    //当前用户权限验证
    public function is_captain(){
        $station_info = M("station")->where("user_id = $this->user_id")->find();
        if(empty($station_info)){
            $this->error("您不是秘书长，无权操作",U("/mobile/station"));
        }elseif($station_info['is_lock']){
            $this->error("您的俱乐部已被暂停使用,无权操",U("/mobile/station"));
        }
        //查询该用户下线人数
        $distribut_list = M("users")->where("station_id = ".$this->user['station_id']." and mobile_validated = 1 and station_status = 1" )->select();
        $distribut_count = count($distribut_list);
//         $goods_distribut_count = 0;//合格下线人数
//         foreach ($distribut_list as $key=>$value){
//             $second_distribut_count = M("users")->where("first_leader =".$value['user_id']." and mobile_validated = 1")->count();
//             if($second_distribut_count >= 30){
//                 $goods_distribut_count ++;
//             }
//             $second_distribut_count = 0;
//         }
        if($distribut_count >=199){
            return true;
        }else{
             $this->error("您的品酒俱乐部尚未正式成立，无权操作",U("/mobile/station"));
        }
    }
    
    public function  edit_des(){
        if(IS_AJAX){
            $station_info = M("station")->where("user_id = $this->user_id and is_lock = 0")->find();
            if(empty($station_info)){
                $back['status'] = 0;
                $back['msg'] = "您不是秘书长,无权操作";
                $back['data'] = "非法操作";
                echo json_encode($back);
                exit();
            }
            $where['des'] = I("post.des");
            $id = I("post.id");
            $result = M("station")->where("id",$id)->save($where);
            if($result){
                $back['status'] = 1;
                $back['msg'] = "操作成功";
                $back['data'] = $result;
            }else{
                $back['status'] = 0;
                $back['msg'] = "操作失败,请重试";
                $back['data'] = "操作失败,请重试试";
            }
        }else{
            $back['status'] = 0;
            $back['msg'] = "非法访问";
            $back['data'] = "非法访问";
        } 
        echo json_encode($back);
        exit();
    }
    
    public function upd_station_pic() {
        $imgsrc = I("post.imgsrc");
        $img = substr(strstr($imgsrc,','),1);
        $data= base64_decode($img);
        $path = "./public/upload/station/Pic/";
        if(!is_dir($path)){
            mkdir($path,777,true);
        }
        $path = $path.$this->user['station_id'].".png";
        $result = file_put_contents($path,$data);
        if($result>0){
            $path = substr_replace($path,'',0,1);
            $result1 = M('station')->where("id",$this->user['station_id'])->save(array("original_img"=>$path));
            if($result1 !== false){
                $back['status'] = 1;
                $back['msg'] = "操作成功";
                $back['data'] = $result1;
            }else{
                $back['status'] = 0;
                $back['msg'] = "操作失败";
                $back['data'] = "数据写入失败";
            }
        }else{
            $back['status'] = 0;
            $back['msg'] = $imgsrc;
            $back['data'] = "头像更换失败";
        }
        echo json_encode($back);
        exit();
    }
    
    
    //退出品酒俱乐部
    public function outStation(){
    	$user_id = $this->user_id;
    	//更新入站日志
		$station_action = M('users')->where(array('user_id'=>$user_id))->getField('station_action');
	    $station_id = M('users')->where(array('user_id'=>$user_id))->getField('station_id');
	    if($station_action){
	    	$act_info = array('act_time'=>time(),
	    	                  'station_id'=>$station_id,
	    	                  'type'=>0,
	    	                  'act_name'=>'退出品酒俱乐部');//type=0则为系统管理员操作，type=1则为用户自己操作
	    	$station_action =  json_decode($station_action,true);
	    	array_push($station_action,$act_info);
	    	$station_action = json_encode($station_action);
	    }
	    else{
	    	$act_info[] = array('act_time'=>time(),
	    	                  'station_id'=>$station_id,
	    	                  'type'=>0,
	    	                  'act_name'=>'退出品酒俱乐部');//type=0则为系统管理员操作，type=1则为用户自己操作
	    	$station_action =  json_encode($act_info,true);
	    }
    	$r = M('users')->where(array('user_id'=>$user_id))->save(array('station_id'=>NULL,'station_status'=>0,'station_leader'=>0,'station_action'=>$station_action));
	    if($r){
	    	$name = M('station')->where(array('id'=>$station_id))->getField('name');
	    	SendMessage(0,1,'您已退出当前俱乐部！当前俱乐部名称：'.$name,array($user_id),'退出俱乐部通知!');
	    	$this->success('退出成功！');
		}
		else{
			$this->error('退出失败！');
		}	
    }
}
?>